"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : mediumblob不支持插入16M的二进制大文件
Create At   : 2022/08/23
@zou_jialiang0501129488
Description :
    1.创建兼容b库
    2.创建配置文件及二进制文件
    3.编译java文件
    4.运行java预编译代码
    5.清理环境
Expect      :
    1.成功
    2.创建配置文件成功
    3.编译java文件成功
    4.运行java文件成功,文件插入失败
    5.清理环境
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class DatatypeTest01(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.com = Common()
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.target_path = os.path.join(os.path.dirname(macro.DB_SCRIPT_PATH),
                                        "JDBC_test")
        self.java_name = "InsertFile"
        self.db_name = 'db_mediumblob0058'

    def test_mediumblob(self):
        step_txt = '-----step1:创建兼容b库并创建表; expect:创建成功-----'
        self.log.info(step_txt)
        sql = f"drop database if exists {self.db_name};" \
              f"create database {self.db_name} dbcompatibility ='B'"
        self.log.info(sql)
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res, 'b库创建失败')
        self.log.info('-----创建mediumblob表-----')
        sql = f"drop table if exists mediumblob0058;" \
              f"create table mediumblob0058(c1 mediumblob);"
        self.log.info(sql)
        res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res, '建表失败')

        step_txt = '-----step2:创建二进制文件插入脚本及二进制文件; expect:创建成功-----'
        self.log.info(step_txt)
        self.com.scp_file(self.pri_root,
                          f"{self.java_name}.java", self.target_path)

        self.log.info('-----创建二进制文件-----')
        file_path = os.path.join(self.target_path, 'mediumblob0058.txt')
        file_cmd = f"cd {self.target_path};" \
                   f"dd if=/dev/zero of=" \
                   f"{file_path} " \
                   f"bs=1K count=16384;" \
                   f"ls {self.target_path}"
        self.log.info(file_cmd)
        res = self.com.get_sh_result(self.pri_root, file_cmd)
        self.assertIn('mediumblob0058.txt', res, '文件创建失败')

        step_txt = '-----step3:编译java文件; expect:编译成功-----'
        self.log.info(step_txt)
        self.pri_root.scp_put(macro.JDBC_PATH,
                              f"{self.target_path}/postgresql.jar")

        java_path = os.path.join(self.target_path, f'{self.java_name}.java')
        sed_cmd = \
            f"sed -i 's#host#localhost#g' {java_path};" \
            f"sed -i 's#dbport#{self.pri_dbuser.db_port}#g' {java_path};" \
            f"sed -i 's#dbname#{self.db_name}#g' {java_path};" \
            f"sed -i 's#table_name#mediumblob0058#g' {java_path};" \
            f"sed -i 's#username#{self.pri_dbuser.ssh_user}#g' {java_path};" \
            f"sed -i 's#password#{macro.COMMON_PASSWD}#g' {java_path};" \
            f"sed -i 's#column#c1#g' {java_path};" \
            f"sed -i 's#file_path#{file_path}#g' {java_path};"
        self.log.info(sed_cmd)
        res = self.com.get_sh_result(self.pri_root, sed_cmd)
        self.assertEqual('', res, '文件内容替换失败')

        cmd = f"javac -encoding utf-8 -cp " \
            f"{os.path.join(self.target_path, 'postgresql.jar')} {java_path}"
        self.log.info(cmd)
        result = self.pri_root.sh(cmd).result()
        self.log.info(result)
        self.assertEqual('', result, "执行失败" + step_txt)

        step_txt = '-----step4:运行java预编译代码; expect:成功-----'
        self.log.info(step_txt)
        cmd = f" java -cp {os.path.join(self.target_path, 'postgresql.jar')}" \
              f":{self.target_path} {self.java_name}"
        self.log.info(cmd)
        res = self.com.get_sh_result(self.pri_root, cmd)
        err_msg = "ERROR: The length: 16777220 bytes is " \
                  "exceeded the max length: 16777215 bytes"
        self.assertIn(err_msg, res, 'java执行失败')

    def tearDown(self):
        step_txt = '-----step5:清理环境; expect:成功-----'
        self.log.info(step_txt)
        cmd = f"drop database if exists {self.db_name};"
        result = self.pri_sh.execut_db_sql(cmd)
        self.log.info(result)
        cmd = f"rm -rf {self.target_path}"
        self.log.info(cmd)
        del_res = self.com.get_sh_result(self.pri_root, cmd)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result, '库删除失败')
        self.assertEqual('', del_res, '文件清理失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
